

lapop12 = readRDS('data/lapop12.rds')

balance = lapop12[ ,c(4,5, 14:15, 320:321, 327, 334:ncol(lapop12))]
J = 24

result_df = matrix(, nrow = J, ncol = 4, byrow = T)
balance = as.data.frame(balance)
balance = balance %>% 
  mutate(income = asinh(income_pool))
balance$income_g = NULL 
balance$income_pool = NULL 
balance = as.data.frame(balance)

for (j in 7:J) {
  
  m = lm_robust(scale(balance[,j]) ~ trial + trend + q2 + q1 , data = balance, 
                fixed_effects = estratopri+estratosec)
  
  result_df[j, 1] = m$coefficients[1]
  result_df[j, 2] = m$conf.high[1]
  result_df[j, 3] = m$conf.low[1]
  result_df[j, 4] = m$std.error[1]
  
}
result_df = as.data.frame(result_df)

result_df = as.data.frame(result_df[7:J,])
colnames(result_df)[1:4] = c('est', 'conf.hi', 'conf.low', 'se')
result_df = result_df %>%  
  na.omit()
result_df$var.name = c('Ideology', 
                       'Catholic', 
                       'Non-religious',
                       'Military Service', 
                       'Employed', 
                       'Income', 
                       'Married', 
                       'White', 
                       'Political Knowledge', 
                       'Owns Computer', 
                       'Single', 
                       'Internet User', 
                       'News Poster', 
                       'Protest', 
                       'Politically Informed', 
                       'Registered to Vote', 
                       'Interested in Politics', 
                       'Support Coup'
                       )

bal_plot = ggplot(result_df, aes(est, var.name)) + 
  geom_point() + 
  geom_errorbar(aes(xmax = conf.hi, xmin = conf.low), width=.1,
               position=position_dodge(.9)) + 
  geom_vline(xintercept = 0, col = 'red', lty = 2) +
  xlab('Standardized Difference') + 
  ylab('Outcome') + 
  theme_bw()

ggsave('fig-out/bal_plot.pdf', width = 5)
ggsave('fig-out/bal_plot.png', width = 5418, height = 2891, units = "px", dpi= 600)

